﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApp.Services;
using MvcJqGrid;
using PagedList;
using System.Globalization;
using WebApp.Data;
using WebApp.Commons;

namespace WebApp.Controllers
{
    public class ProjectController : Controller
    {
        //
        private ProjectService _service;
        // GET: /Project/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult CreateProject()
        {
            return View();
        }

        public ActionResult AreaTypeStep()
        {
            return View();
        }

        public ActionResult Area()
        {
            return View();
        }


        #region PongAdded

        public ActionResult Project()
        {
            return View();
        }

        public ActionResult NewProject()
        {
            return View();
        }

        #endregion

        //controller
        public JsonResult List(GridSettings gridSettings, string firstName, string tel, string address, string date)
        {
            _service = new ProjectService();

            var realCount = 0;

            var datas = _service.GetAreaType(gridSettings.PageIndex,
                                          gridSettings.PageSize,
                                          gridSettings.SortColumn,
                                          gridSettings.SortOrder,
                                          firstName,tel,address,date,
                                          ref realCount);

            var totalBUs = realCount;
            var totalPage = totalBUs / gridSettings.PageSize;
            
            if (totalPage > Convert.ToInt32(totalPage))
            {
                totalPage = totalPage + 1;
            }
            var jsonData = new
            {
                total = Convert.ToInt32(totalPage),
                page = gridSettings.PageIndex,
                records = totalBUs,
                rows = (
                            from c in datas
                            select new
                            {
                                id = c.project_id,
                                cell = new[] {
                                            c.project_id.ToString(CultureInfo.InvariantCulture),
                                            c.project_location,
                                            _service.GetCustName(c.customer_id).customer_firstname,
                                            c.project_create_date.HasValue ? c.project_create_date.Value.ToLongDateString() : ""
                                            }
                            }
                        ).ToPagedList(1, gridSettings.PageSize).ToArray()
            };

            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetProjectByCustomerId(GridSettings gridSettings, string custId)
        {
            _service = new ProjectService();

            var realCount = 0;

            var datas = _service.GetProjectListByCustId(gridSettings.PageIndex,
                                          gridSettings.PageSize,
                                          gridSettings.SortColumn,
                                          gridSettings.SortOrder,
                                          custId,
                                          ref realCount);

            var totalBUs = realCount;
            var totalPage = totalBUs / gridSettings.PageSize;

            if (totalPage > Convert.ToInt32(totalPage))
            {
                totalPage = totalPage + 1;
            }
            var jsonData = new
            {
                total = Convert.ToInt32(totalPage),
                page = gridSettings.PageIndex,
                records = totalBUs,
                rows = (
                            from c in datas
                            select new
                            {
                                id = c.project_id,
                                cell = new[] {
                                            c.project_id.ToString(CultureInfo.InvariantCulture),
                                            c.project_location,
                                            _service.GetCustName(c.customer_id).customer_firstname,
                                            c.project_create_date.HasValue ? c.project_create_date.Value.ToLongDateString() : ""
                                            }
                            }
                        ).ToPagedList(1, gridSettings.PageSize).ToArray()
            };

            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

        public JsonResult Update(tbl_project oModel, FormCollection formCollection)
        {
            _service = new ProjectService();

            var isPass = false;
            var iMessage = string.Empty;

            var operation = formCollection["oper"];

            if (operation.Equals("add") || operation.Equals("edit"))
            {
                isPass = _service.Update(oModel);
            }

            return Json(new { isPass, iMessage });
        }

        public JsonResult Delete(int[] ids, FormCollection formCollection)
        {
            _service = new ProjectService();

            var isPass = false;
            var iMessage = string.Empty;

            var operation = formCollection["oper"];

            if (operation.Equals("del"))
            {
                isPass = _service.Delete(ids);
            }

            return Json(new { isPass, iMessage });
        }

        public String GetCust()
        {
            var sTag = DataProvider.GetCustomer().Aggregate("<select>", (current, type) => current + "<option value='" + type.customer_id + "'>" + type.customer_firstname + " " + type.customer_lastname + "</option>");

            return sTag + "</select>";
        }

    }
}
